package com.yx;

import com.yx.model.ProductInfo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author yx
 * @date 2025-04-06 10:35
 * @description:
 */
public class PreLoader {

    private final FutureTask<ProductInfo> futureTask = new FutureTask<>(new Callable<ProductInfo>() {
        @Override
        public ProductInfo call() throws Exception {
            return loadProductInfo();
        }
    });

    private ProductInfo loadProductInfo() {
        return new ProductInfo();
    }

    private final Thread thread = new Thread(futureTask);

    public void start() {
        thread.start();
    }

    private ProductInfo get() {
        try {
            return futureTask.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        PreLoader preLoader = new PreLoader();
        preLoader.start();
        ProductInfo productInfo = preLoader.get();
        System.out.println(productInfo);
    }

}
